import { my_request, my_showToast_error } from '../../utils/my_utils'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    meetingList: wx.getStorageSync("index-meetingList"),
    select_status: 1,
  },
  getMeeting: function () {
    var self = this
    my_request({
      url: 'https://sichuangyi.demopp.in/api-conference-alpha/conference-digest',
      method: "GET",
      success(res) {
        
        if (res.data.data.conference.length > 0) {
          self.setData({ has_meetingList: true })
        } else {
          self.setData({ has_meetingListh: false })
        }
        let meetingList = res.data.data.conference
        
        meetingList = meetingList.map(m => {
          let curState = self.stateFliter(m)
          return {
            ...m,
            stateText: curState.txt,
            stateClass: curState.class
          }
        })

        self.setData({
          meetingList
        })

        wx.setStorageSync("index-meetingList", meetingList)

      },
      fail: function (res) {
        console.log(res)
        if (res.statusCode === 403) {
            wx.redirectTo({ url: '/pages/login/login?next=/pages/Activity/index' });
        }
        else {
          my_showToast_error("获取异常", 2000)
          
        }
      }
    })
  },
  stateFliter(meeting) {
    switch(meeting.state) {
      case 1:
        return {
          txt: '报名中',
          class: 'bg-red'
        }
      case 2:
        return {
          txt: '进行中',
          class: 'bg-green'
        }
      case 3:
        return {
          txt: '已结束',
          class: 'bg-grey'
        }
      default:
        return {
          txt: '审核中',
          class: 'bg-grey'
        }
    }
  },
  nav: function (e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  onShow: function () {
    if (!wx.getStorageSync("auth_token")) {
      wx.redirectTo({ url: '/pages/login/login?next=/pages/Activity/index' });
    } else {
      this.getMeeting();
    }
  },
  onPullDownRefresh() {
    this.getMeeting();
  }
})
